﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ecopsy.DataTier;
using AgencyEntityLib.Context ;
using System.Threading;
namespace AgencyEntityLib.BaseEntity
{
    /// <summary>
    /// Рекламный носитель
    /// 
    /// </summary>
    public class Constraction : BusinessRulesEntity, ISqlSignature
    {
        /// <summary>
        /// Тип конструкции - 
        /// описывается как связанный список элементов (тип конструкции - тип - значение)
        /// </summary>            

        private TreeDictionaryItemValue _ConstractionType = null;
        public TreeDictionaryItemValue  ConstractionType
        {
            get
            {
                return _ConstractionType;
            }
            set
            {
                _ConstractionType = value;                
            }
        }

        public bool IsPrizmaVision 
        {
            get 
            {
                var soldOutProducts =
                from p in this.SideList .ToArray<Side >()  
                where p.SurfaceCount  > 1
                select p;

                return soldOutProducts.ToArray<Side>().Length > 0;   
    
            }
        }

        private TreeDictionaryItemValue _LocationDescribe = null;
        public TreeDictionaryItemValue LocationDescribe
        {
            get
            {
                return _LocationDescribe;
            }
            set
            {
                _LocationDescribe = value;
            }
        }

        private TreeDictionaryItemValue _AdressProgramm = null;     
        public TreeDictionaryItemValue AdressProgramm
        {
            get
            {                
                return _AdressProgramm ;
            }
            set
            {
                _AdressProgramm = value;
            }
        }

        private TreeDictionaryItemValue _ConstractionCondition = null;
        public TreeDictionaryItemValue ConstractionCondition
        {
            get
            {
                return _ConstractionCondition;
            }
            set
            {
                _ConstractionCondition = value;
            }
        }

        private TreeDictionaryItemValue _GeoBinding = null;
        public TreeDictionaryItemValue GeoBinding
        {
            get
            {

                return _GeoBinding;
            }
            set
            {
                _GeoBinding = value;
            }
        }

        private string _Streets = string.Empty;
        public string  Streets
        {
            get
            {
                return _Streets ;
            }
            set
            {
                _Streets = value;
            }
        }

        private string _GID = string.Empty;
        /// <summary>
        /// Ид конструкции в варианте агентсва
        /// </summary>
        public string GID 
        {  get { return _GID; } 
            set {
                bool bOk = this.BusinesRulesEngine.OwnerChanging( "GID", value);
                if(bOk)
                    _GID = value; 
                }
            }

        private DateTime  _dtCreate = DateTime.Now  ;
        /// <summary>
        /// Датат монтажа конструкции
        /// </summary>
        public DateTime dtCreate { get { return _dtCreate; } set { _dtCreate = value; } }


        private DateTime _dtDestroy = AgencyLib_GlobalConstants.MaxDate;
        /// <summary>
        /// Дата демонтажа 
        /// </summary>
        public DateTime dtDestroy { get { return _dtDestroy; } set { _dtDestroy = value; } }

        private string _Light=string.Empty ;
        /// <summary>
        /// Освещенность  338EA30A-14CB-4BE1-B6A1-0958385162E3
        /// </summary>        
        public string Light 
        { 
            get { return _Light; } set { _Light = value; } 
        }

        [EntityNonSerialized]
        public bool  bLight
        {
            get { return (_Light.Length!=0) ; }
            set { _Light = (value?"338EA30A-14CB-4BE1-B6A1-0958385162E3":string.Empty); }
        }

        private SideList _SideList = new SideList();
        /// <summary>
        /// стороны носителя
        /// </summary>
        [ImReportItem]
        public SideList SideList { get { return _SideList; } set { _SideList = value; } }

        
        private  string _DepartmentID = null;        
        public   string DepartmentID {get{return  _DepartmentID;} set{_DepartmentID= value;}}


        private string _MapX = string .Empty ;
        public string MapX { get { return _MapX; } set { _MapX = value; } }

        private string _MapY = string.Empty;
        public string MapY { get { return _MapY; } set { _MapY = value; } }


        private string _Describe = null;
        public string Describe { get { return _Describe; } set { _Describe = value; } }

        #region ISqlSignature Members

        [EntityNonSerialized]
        public string SaveProcedure
        {
            get { return "ConstractionSave"; }
        }
        
        [EntityNonSerialized]
        public string DeleteProcedure
        {
            get { throw new NotImplementedException(); }
        }
        
        [EntityNonSerialized]
        public string LoadProcedure
        {
            get { return "ConstractionLoad"; }
        }

        #endregion

        [EntityNonSerialized ]
        protected override string ReportTypeExt
        {
            get
            {
                return "AgencyEntityLib.BaseEntity.ConstractionExt";
            }
        }

        /// <summary>
        /// Параметр используемый исключительно дя загрузки конструкции по ид стороны
        /// </summary>
        [EntityNonSerialized]
        public string SideID
        {
            get;
            set;
        }

        public override string ImAsXML
        {
            get
            {
                return base.ImAsXML;
            }
        }

        //public override void Save()
        //{
            
        ////    if (this.ID.IndexOf(AgencyLib_GlobalConstants.qPrefNEW_RECORD) > -1 || this.State.Equals(EntityState.Deleted))
        ////    {
        ////        base.Save();
        ////        return;
        ////    }
        ////    // сбрасываем состояние объекта в Загружен                       
        ////    foreach (Side s in this.SideList)
        ////    {
        ////        s.SetHash();
        ////    }
        ////    this.SetHash();

        ////    base.Save (AppContext.GetSC().ConnectionString);


             
            
        //}

        public string GetTxtStreets()
        {
            return Streets;
        }
        
    }

    public class SideList:EntityList 
    {
        /// <summary>
        /// Диапазон литер для стороны
        /// </summary>
        public static string[] Liters = { "A", "B", "C", "D", "E", "F" };
        public override void Add(object obj)
        {
            if(this.Count>Liters.Length )
                throw new Exception (string.Format ("Количество сторон не может превышать {0} ",Liters.Length));

            (obj as Side).Litera = Liters[this.Count];
            base.Add(obj);
        }

    }


}
